/**
 * Copyright 2009 Henning Teek, Ingo Renner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.chigscommons.web.common.modal;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.model.IModel;


/**
 * 
 * @author Ingo Renner
 * @author Henning Teek
 *
 * @param <T>
 */
public abstract class AModalWindow<T> extends ModalWindow {
	private IModel<T> model;
	
	public AModalWindow(String id,  IModel<T> model) {
		super(id);
		this.model = model;
		// Kann später auch noch neu gesetzt werden, jetzt nur Bequemlichkeit für default Größe.
		setInitialWidth(650);
        setInitialHeight(400);
	}

	public IModel<T> getModel() {
		return model;
	}

	public void setModel(IModel<T> model) {
		this.model = model;
	}

	protected abstract  void afterCanel(AjaxRequestTarget target);

	protected abstract  void afterSubmit(AjaxRequestTarget target, IModel<T> model);

	protected abstract Component fillForm(String componentId, IModel<T> model);
	
	@Override
	public void onBeforeRender() {
		if (!hasBeenRendered()){
			setContent(createContentPanel());
		}
		super.onBeforeRender();
	}

	protected Component createContentPanel() {
		return new AModalWindowContentPanel<T>(this.getContentId(), getModel()){
			@Override
			protected void afterCanel(AjaxRequestTarget target) {
				AModalWindow.this.afterCanel(target);
			}
			@Override
			protected void afterSubmit(AjaxRequestTarget target, IModel<T> model) {
				AModalWindow.this.afterSubmit(target, model);
			}
			@Override
			protected Component fillForm(String componentId, IModel<T> model) {
				return AModalWindow.this.fillForm(componentId, model);
			}
		};
	}

}
